var tree = {
    //  加载树形列表数据
    loadTreeData: function (obj, dataType) {
        var keyTo = "";
        //  折叠位置
        switch (dataType) {
            case "menus":
                keyTo = "menuname";
                break;
            case "depts":
                keyTo = "deptname";
                break;
            case "jobs":
                keyTo = "jobname";
                break;
            case "dics":
                keyTo = "dicname";
                break;
            case "sources":
                keyTo = "title";
                break;
            case "maketaskuser":
                keyTo = "title";
        }
        document.querySelector(obj).GM({
            gridManagerName: 'table-tree',
            ajaxData: com.loadUrl(dataType, 2),
            supportCheckbox: false, //  是否多选
            supportAutoOrder: false,    // 是否支持自动序号
            supportAdjust: false,   //  用于配置是否支持宽度调整功能
            supportDrag: false, //  用于配置是否支持列的拖拽功能
            columnData: tree.loadTreeColumn(dataType),
            supportTreeData: true,  //  用于配置是否支持树型表格，配置树型数据后行移动功能将无效
            minHeight: '600px',
            treeConfig: {
                // 树展开操作按键所属容器，此处配置columnData的key值。未配置时，将默认选择columnData的第一项
                insertTo: keyTo,
                // 初始将所有数据展开, 默认为false
                openState: dataType == 'jobs' ? true : false,
                // 子节点关键字，默认为'children'
                treeKey: 'children'
            },
            query: tree.loadParas(dataType),
            ajaxSuccess: function(response){
                //  请求成功后设置tip提示
                $("[data-tooltip='tooltip']").tooltip();
            },
        });
    },
    //  加载树形列
    loadTreeColumn: function (dataType) {
        var treeColumn = [];
        switch (dataType) {
            case "menus":
                treeColumn = [
                    {
                        key: 'sn',
                        text: function () {
                            return '<input type="checkbox" name=\'checkAll\' onclick="com.checkAlls()">';
                        },
                        width: '40px',
                        align: 'center',
                        template: function (cell, row, index, key) {
                            return '<input type="checkbox" name=\'checkItems\' onclick="com.checkItems()" value="' + row.menucode + '">';
                        }
                    },
                    {
                        key: 'menuname',
                        text: '菜单名称'
                    },
                    {
                        key: 'menutype',
                        text: '菜单类型',
                        template: function (cell, row, index, key) {
                            return cell == 0 ? com.loadSpan('功能', 'primary') : com.loadSpan('权限', 'danger');
                        }
                    },
                    {
                        key: 'menucode',
                        text: '菜单编码'
                    },
                    {
                        key: 'jumpurl',
                        text: '跳转路径'
                    },
                    {
                        key: 'isEffective',
                        text: '状态',
                        template: function (cell, row, index, key) {
                            return cell == 0 ? com.loadSpan('关闭', 'danger') : com.loadSpan('开启', 'primary');
                        }
                    },
                    {
                        key: 'orderbyid',
                        text: '排序',
                        width: '100px'
                    },
                    {
                        key: 'operate',
                        text: '操作',
                        width: '100px',
                        template: function (cell, row, index, key) {
                            return com.loadBtIcon('edit', row) +
                                // com.loadBtIcon('delete', row.menucode, 'menus') +
                                com.loadBtIcon('desc', row, 'menus') +
                                com.loadBtIcon('asc', row, 'menus');
                        }
                    }
                ];
                break;
            case "depts":
                treeColumn = [
                    {
                        key: 'sn',
                        text: function () {
                            return '<input type="checkbox" name=\'checkAll\' onclick="com.checkAlls()">';
                        },
                        width: '40px',
                        align: 'center',
                        template: function (cell, row, index, key) {
                            return '<input type="checkbox" name=\'checkItems\' onclick="com.checkItems()" value="' + row.deptcode + '">';
                        }
                    },
                    {
                        key: 'deptname',
                        text: '部门名称'
                    },
                    {
                        key: 'deptcode',
                        text: '部门编码'
                    },
                    {
                        key: 'operate',
                        text: '操作',
                        width: '100px',
                        template: function (cell, row, index, key) {
                            return com.loadBtIcon('edit', row) +
                                com.loadBtIcon('delete', row.deptcode, 'depts');
                        }
                    }
                ];
                break;
            case "jobs":
                treeColumn = [
                    {
                        key: 'sn',
                        text: function () {
                            return '<input type="checkbox" name=\'checkAll\' onclick="com.checkAlls()">';
                        },
                        width: '40px',
                        align: 'center',
                        template: function (cell, row, index, key) {
                            return '<input type="checkbox" name=\'checkItems\' onclick="com.checkItems()" value="' + row.jobcode + '">';
                        }
                    },
                    {
                        key: 'jobname',
                        text: '职务名称'
                    },
                    {
                        key: 'jobcode',
                        text: '职务编码'
                    },
                    {
                        key: 'menus',
                        text: '权限',
                        width: '100px',
                        template: function (cell, row, index, key) {
                            return "<a data-toggle=\"modal\" data-target=\"#modal-menus\" onclick=\"tree.setJobCode('" + row.jobcode + "')\" style='font-size: 15px; margin-left: 5px '><i class='fa fa-gear'></i></a>";
                        }
                    },
                    {
                        key: 'operate',
                        text: '操作',
                        width: '100px',
                        template: function (cell, row, index, key) {
                            return com.loadBtIcon('edit', row) +
                                com.loadBtIcon('delete', row.jobcode, 'jobs');
                        }
                    }
                ];
                break;
            case "dics":
                treeColumn = [
                    {
                        key: 'sn',
                        text: function () {
                            return '<input type="checkbox" name=\'checkAll\' onclick="com.checkAlls()">';
                        },
                        width: '40px',
                        align: 'center',
                        template: function (cell, row, index, key) {
                            return '<input type="checkbox" name=\'checkItems\' onclick="com.checkItems()" value="' + row.diccode + '">';
                        }
                    },
                    {
                        key: 'dicname',
                        text: '字典名称'
                    },
                    {
                        key: 'diccode',
                        text: '字典编码'
                    },
                    {
                        key: 'operate',
                        text: '操作',
                        width: '100px',
                        template: function (cell, row, index, key) {
                            return com.loadBtIcon('edit', row) +
                                com.loadBtIcon('delete', row.diccode, 'dics');
                        }
                    }
                ];
                break;
            case "sources":
                treeColumn = [
                    {
                        key: 'sn',
                        text: function () {
                            return '<input type="checkbox" name=\'checkAll\' onclick="com.checkAlls()">';
                        },
                        width: '40px',
                        align: 'center',
                        template: function (cell, row, index, key) {
                            return '<input type="checkbox" name=\'checkItems\' onclick="com.checkItems()" value="' + row.code + '">';
                        }
                    },
                    {
                        key: 'title',
                        text: '来源名称'
                    },
                    {
                        key: 'code',
                        text: '来源编码'
                    },
                    {
                        key: 'orderbyid',
                        text: '排序ID'
                    },
                    {
                        key: 'operate',
                        text: '操作',
                        width: '100px',
                        template: function (cell, row, index, key) {
                            return com.loadBtIcon('edit', row) +
                                com.loadBtIcon('delete', row.code, 'sources');
                        }
                    }
                ];
                break;
        }
        return treeColumn;
    },
    //  设置职务编码
    setJobCode: function (jobCode) {
        $("#Hidjobcode").val(jobCode);
    },
    //  加载树形下拉选项
    loadTreeSelect: function (obj, dataType, isCheck, inputId, treeDom) {
        $(obj).treeSelect({
            data: tree.loadTreeSelectData(dataType),
            inputId: inputId || "txtPname",
            isCheck: isCheck || false,
            treeDom: treeDom || "_treeDom",
            obj: obj,
            dataType: dataType
        });
    },
    //  加载树形下拉结构数据源
    loadTreeSelectData: function (dataType) {
        var treeData = [];
        var url = "../com/querySelectDatas";
        var data = {dataType: dataType};
        switch (dataType) {
            case "jobMenus":
                data = {dataType: dataType, jobcode: $("#Hidjobcode").val()};
                break;
            case "userMenus":
                data = {dataType: dataType, userid: $("#userid").val()};
                break;
            case "userDepts":
                data = {dataType: dataType, userid: $("#userid").val()};
                break;
            case "noticDepts":
                data = {dataType: dataType, noticeid: $("#id").val()};
                break;
            case "jobs":
            case "userJobs":
            case "queryJobs":
                data = {dataType: dataType, deptcode: $("#Hiddeptcode").val()};
                break;
            case "maketaskuser":
                data = {dataType: dataType, deptname: JSON.parse($.cookie("users")).deptname};
        }
        console.log(data)
        $.ajax({
            url: url,
            type: 'get',
            dataType: 'json',
            async: false,
            data: data,
            success: function (data) {
                console.log(data)
                //  增加默认根级显示
                switch (dataType) {
                    case "depts":
                    case "jobs":
                    case "menus":
                    case "dics":
                    case "sources":
                        var item = {name: "根级", pId: '00', id: ""};
                        treeData.push(item);
                        break;
                    case "maketaskuser":
                        var item = {name: "根级"};
                        treeData.push(item);
                        break;
                }
                $(data).each(function (i, e) {
                    var name = e.name;
                    var id = e.id;
                    var item = {};
                    switch (dataType) {
                        // case "menus":
                        case "userMenus":
                        case "jobMenus":
                            name = e.menuname;
                            id = e.menucode;
                            break;
                        case "depts":
                        case "userDepts":
                        case "noticDepts":
                        case "orderTypeDepts":
                            name = e.deptname;
                            id = e.deptcode;
                            break;
                        case "jobs":
                        case "userJobs":
                        case "queryJobs":
                            name = e.jobname;
                            id = e.jobcode;
                            break;
                    }
                    item = {name: name, pId: e.parentcode, id: id};
                    switch (dataType) {
                        case "jobMenus":
                        case "userDepts":
                            item["checked"] = e.haves ? true : false;
                            break;
                        case "userMenus":
                            item["checked"] = e.uhaves ? true : false;
                            break;
                        case "noticDepts":
                            item["checked"] = e.nauth ? true : false;
                            break;
                    }
                    treeData.push(item);
                });
            }
        });
        return treeData;
    },
    //  刷新树形下拉数据
    reloadTreeSelectData: function (dataType, inputId, treeDom) {
        treeDom = treeDom || "_treeDom";
        inputId = inputId || "txtPname";
        // console.log(treeDom , inputId)
        var setting = {
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: 0
                }
            },
            callback: {
                beforeClick: function (treeId, treeNode, clickFlag) {
                    //  点击前事件
                    switch (dataType) {
                        //  菜单管理
                        case "menus":
                            if (treeNode.id && treeNode.id == $("#menucode").val()) {
                                return false;
                            }
                            break;
                        case "dics":
                            if (treeNode.id && treeNode.id == $("#diccode").val()) {
                                return false;
                            }
                            break;
                    }
                },
                onClick: function (event, treeId, treeNode) {
                    //  点击事件
                    $("#" + inputId).val(treeNode.name);
                    switch (dataType) {
                        case "userJobs":
                            $("#jobcode").val(treeNode.id);
                            break;
                        case "queryJobs":
                            $("#queryJobCode").val(treeNode.id);
                            break;
                        case "orderTypeDepts":
                            $("#deptcode").val(treeNode.id);
                            break;
                        case "maketaskuser":
                            $("#")
                        default:
                            $("#parentcode").val(treeNode.id);
                            break;
                    }
                }
            }
        };
        var opts = {
            data: tree.loadTreeSelectData(dataType),
            inputId: inputId
        };
        $.fn.zTree.init($("#" + treeDom), setting, opts.data);
    },
    //  加载参数
    loadParas: function (dataType) {
        var dataParas = {sName: $("#querySeachName").val()};
        switch (dataType) {
            case "jobs":
                dataParas = {
                    deptcode: $("#Hiddeptcode").val(),
                    jobname: $("#querySeachName").val(),
                }
                break;
            case "tasklst":
                dataParas = {
                   taskname: $("#querySeachName").val()
                }
        }
        return dataParas;
    },
    //  查询树形数据
    queryTreeData: function (dataType) {
        var _query = tree.loadParas(dataType);
        document.querySelector('#table-tree').GM('setQuery', _query);
    },
    //  加载左侧树形菜单
    loadTreeJs: function (obj, dataType) {
        $(obj).jstree({
            "core": {
                "themes": {
                    "icons": false
                },
                "data": {
                    'url': "../depts/queryTreeJs",
                    'data': function (node) {
                        return node;
                    }
                }
            }

        }).on('select_node.jstree', function (el, data) {
            //  选择事件
            $("#Hiddeptcode").val(data.node.original.deptcode);
            $("#Hiddeptname").val(data.node.original.text);
            switch (dataType) {
                case "jobs":
                    tree.queryTreeData('jobs');
                    break;
                case "users":
                    table.queryData('users');
                    //  重新渲染职务数据
                    $("#queryJobCode").val("");
                    $("#queryJobName").val("");
                    tree.reloadTreeSelectData('queryJobs', 'queryJobName', 'q-TreeDom');
                    break;
            }
        }).on("changed.jstree", function (el, data) {
            //  数据改变事件
        }).on('loaded.jstree', function (e, data) {
            //  初始化后的数据
            var depts = $(obj).jstree(true).get_selected(true)[0].original;
            $("#Hiddeptcode").val(depts.deptcode);
            $("#Hiddeptname").val(depts.text);

            switch (dataType) {
                case "users":
                    table.loadData('#table-data', 'users');
                    //  筛选条件
                    tree.loadTreeSelect("#query-tree", 'queryJobs', 0, 'queryJobName', 'q-TreeDom');
                    break
                case "jobs":
                    tree.loadTreeData('#table-tree', 'jobs');
                    break
            }
        });
    },
    //  获取权限设置数据
    queryTreeAuto: function (dataType) {
        var setting = {
            view: {
                showIcon: true
            },
            check: {
                enable: true,
                chkboxType: dataType == "noticDepts" ? {"Y": "", "N": ""} : {"Y": "ps", "N": "ps"}
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                onCheck: function (event, treeId, treeNode) {
                    var treeObj = $.fn.zTree.getZTreeObj("_treeDom"),
                        nodes = treeObj.getCheckedNodes(true),
                        v = [], n = [];
                    for (var i = 0; i < nodes.length; i++) {
                        v.push(nodes[i].id);
                        n.push(nodes[i].name);
                    }
                    $("#txtPname").val(n.join(','));
                    $("#treecodes").val(v.join(','));
                }
            }
        };
        var obj = "treeMenus";
        switch (dataType) {
            case "userDepts":
                obj = "treeDepts";
                break;
            case "noticDepts":
                obj = "_treeDom";
                break;
        }
        var opts = {
            data: tree.loadTreeSelectData(dataType),
            inputId: "txtPname"
        };
        $.fn.zTree.init($("#" + obj), setting, opts.data);
    },
};